package com.changhong.wlyspectrum.MainTestActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.changhong.wlyspectrum.R;
import com.changhong.wlyspectrum.utils.MyConfig;

/**
 * Created by Mr_tao
 * on 2017/3/9.
 */
public class InfoInputActivity extends Activity{
    private final static String TAG = "InfoInputActivity";
    private ImageView mBackIv;
    private TextView mUserIdTv;
    private TextView mTimeTv;
    private EditText mAcrNumEdt;
    private EditText mRowNumEdt;
    private EditText mKilnNumEdt;
    private EditText mLevelEdt;
    private EditText mIOEdt;
    private Button mNextBtn;
    private String collectionId;
    private String btn_1;
    private String btn_2;
    private String title_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_input_layout);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar_info_input);

        InitData();
        InitUi();
    }

    private void InitData(){
        Intent intent = getIntent();
        collectionId = intent.getStringExtra("collectionId");
        title_name = intent.getStringExtra("title_name");
        btn_1 = intent.getStringExtra("btn_1");
        btn_2 = intent.getStringExtra("btn_2");
    }
    private void InitUi() {
        mBackIv = (ImageView)findViewById(R.id.alcohol2login);
        mBackIv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        SharedPreferences mypref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String user_id = mypref.getString("user_id", "");
        mUserIdTv = (TextView)findViewById(R.id.tv_id);
        mUserIdTv.setText("当前用户：" + user_id);

        mTimeTv = (TextView)findViewById(R.id.tv_time);
        mTimeTv.setText(MyConfig.gettime());

        mAcrNumEdt = (EditText)findViewById(R.id.mkuahao);
        mAcrNumEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mAcrNumEdt.setHint(null);
                } else {
                    if (mAcrNumEdt.getText().length() != 0) {
                        mAcrNumEdt.setHint(null);
                    } else {
                        mAcrNumEdt.setHint(R.string.input_kuahao);
                    }
                }

            }
        });
        mAcrNumEdt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mAcrNumEdt.getText().length() != 0) {
                    mAcrNumEdt.setHint(null);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mRowNumEdt = (EditText)findViewById(R.id.mpaihao);
        mRowNumEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mRowNumEdt.setHint(null);
                } else {
                    if (mRowNumEdt.getText().length() != 0) {
                        mRowNumEdt.setHint(null);
                    } else {
                        mRowNumEdt.setHint(R.string.input_paihao);
                    }
                }

            }
        });
        mRowNumEdt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mRowNumEdt.getText().length() != 0) {
                    mRowNumEdt.setHint(null);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mKilnNumEdt = (EditText)findViewById(R.id.myaohao);
        mKilnNumEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mKilnNumEdt.setHint(null);
                } else {
                    if (mKilnNumEdt.getText().length() != 0) {
                        mKilnNumEdt.setHint(null);
                    } else {
                        mKilnNumEdt.setHint(R.string.input_yaohao);
                    }
                }

            }
        });
        mKilnNumEdt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mKilnNumEdt.getText().length() != 0) {
                    mKilnNumEdt.setHint(null);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mLevelEdt = (EditText)findViewById(R.id.mcengci);
        mLevelEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mLevelEdt.setHint(null);
                } else {
                    if (mLevelEdt.getText().length() != 0) {
                        mLevelEdt.setHint(null);
                    } else {
                        mLevelEdt.setHint(R.string.input_cengci);
                    }
                }

            }
        });
        mLevelEdt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mLevelEdt.getText().length() != 0) {
                    mLevelEdt.setHint(null);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mIOEdt = (EditText)findViewById(R.id.mchuru);
        mIOEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mIOEdt.setHint(null);
                } else {
                    if (mIOEdt.getText().length() != 0) {
                        mIOEdt.setHint(null);
                    } else {
                        mIOEdt.setHint(R.string.input_churu);
                    }
                }

            }
        });
        mIOEdt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mIOEdt.getText().length() != 0) {
                    mIOEdt.setHint(null);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mNextBtn = (Button)findViewById(R.id.alcohol_next_btn);
        mNextBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String acrnum = mAcrNumEdt.getText().toString();
                String rownum = mRowNumEdt.getText().toString();
                String kilnnum = mKilnNumEdt.getText().toString();
                String levelnum = mLevelEdt.getText().toString();
                String ionum = mIOEdt.getText().toString();
                if(!acrnum.isEmpty()){
                    if(!rownum.isEmpty()){
                        if(!kilnnum.isEmpty()){
                            if(!levelnum.isEmpty()){
                                if(!ionum.isEmpty()){
                                    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                                    SharedPreferences.Editor editor = pref.edit();
                                    editor.putString("kuahao", acrnum);
                                    editor.putString("paihao", rownum);
                                    editor.putString("yaohao", kilnnum);
                                    editor.putString("cengci", levelnum);
                                    editor.putString("churu", ionum);
                                    editor.commit();
                                    Intent intent = new Intent(InfoInputActivity.this,MainTestActivity.class);
                                    intent.putExtra("collectionId",collectionId);
                                    intent.putExtra("title_name",getString(R.string.wuliangye));
                                    intent.putExtra("btn_1",getString(R.string.alcohol_analyse));
                                    intent.putExtra("btn_2",getString(R.string.alcohol_model));
                                    startActivity(intent);
                                }else{
                                    Toast.makeText(getApplicationContext(), R.string.input_churu, Toast.LENGTH_SHORT).show();
                                }
                            }else{
                                Toast.makeText(getApplicationContext(), R.string.input_cengci, Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            Toast.makeText(getApplicationContext(), R.string.input_yaohao, Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(getApplicationContext(), R.string.input_paihao, Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(), R.string.input_kuahao, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
